package com.diandian.backend.entrance.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.diandian.backend.entrance.client.DianDianKuaiShouLifeExchangeClient;
import com.diandian.backend.entrance.client.dto.CertificateCancelParamDTO;
import com.diandian.backend.entrance.client.dto.CertificatePrepareParamDTO;
import com.diandian.backend.entrance.client.dto.CertificateVerifyParamDTO;
import com.diandian.backend.entrance.constants.EntranceConstant;
import com.diandian.backend.entrance.dto.ClientPermissionCacheDataDTO;
import com.diandian.backend.entrance.dto.KuaiShouLifeTokenGenerateDTO;
import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.BaseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RouterKuaiShouLifeService {

    private static final Logger logger = LoggerFactory.getLogger(RouterKuaiShouLifeService.class);

    @Autowired
    private DianDianKuaiShouLifeExchangeClient kuaiShouLifeClient;

    /**
     * 子路由服务
     * @param clientKey
     * @param clientSecret
     * @param data
     * {
     *     "cmd":"sms.send"
     * }
     * @return
     */
    public Object execute(ClientPermissionCacheDataDTO clientInfoDTO, String clientKey, String clientSecret, String cmd, JSONObject data) {

        // 获取token
        if(EntranceConstant.ROUTER_KUAISHOU_GET_TOKEN.equals(cmd)){

            logger.info("执行accessToken获取请求，参数: {}", data == null ? "" : data.toJSONString());

            // 获取配置信息
            String appKey = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE, EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE_APPKEY);
            String appSecret = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE, EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE_APPSECRET);
            String merchantId = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE, EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE_MERCHANTID);
            if(appKey == null || "".equals(appKey) || appSecret == null || "".equals(appSecret) || merchantId == null || "".equals(merchantId)){
                throw new DefaultException("配置缺失");
            }

            // 执行请求
            BaseResult resultDTO = kuaiShouLifeClient.getToken(appKey, appSecret, merchantId);

            logger.info("执行accessToken获取请求结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        if(EntranceConstant.ROUTER_KUAISHOU_GET_GENERATE.equals(cmd)){

            logger.info("执行accessToken生成请求，参数: {}", data == null ? "" : data.toJSONString());

            // 获取配置信息
            String appKey = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE, EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE_APPKEY);
            String appSecret = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE, EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE_APPSECRET);
            if(appKey == null || "".equals(appKey) || appSecret == null || "".equals(appSecret)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            if(data == null){
                throw new DefaultException("参数缺失");
            }
            KuaiShouLifeTokenGenerateDTO dataDTO = JSONObject.parseObject(data.toString(), KuaiShouLifeTokenGenerateDTO.class);

            // 执行短信发送
            BaseResult resultDTO = kuaiShouLifeClient.generateToken(appKey, appSecret, dataDTO.getCode(), dataDTO.getState());

            logger.info("执行accessToken生成请求结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        // 验券准备
        if(EntranceConstant.ROUTER_KUAISHOU_CERTIFICATE_PREPARE.equals(cmd)){

            logger.info("执行验券准备请求，参数: {}", data == null ? "" : data.toJSONString());

            // 获取配置信息
            String appKey = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE, EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE_APPKEY);
            String appSecret = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE, EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE_APPSECRET);
            String merchantId = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE, EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE_MERCHANTID);
            if(appKey == null || "".equals(appKey) || appSecret == null || "".equals(appSecret) || merchantId == null || "".equals(merchantId)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            if(data == null){
                throw new DefaultException("参数缺失");
            }
            CertificatePrepareParamDTO dataDTO = JSONObject.parseObject(data.toString(), CertificatePrepareParamDTO.class);

            // 执行短信发送
            BaseResult resultDTO = kuaiShouLifeClient.prepare(appKey, appSecret, merchantId, dataDTO);

            logger.info("执行验券准备请求结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        // 验券准备（返回内容包含订单详情）
        if(EntranceConstant.ROUTER_KUAISHOU_CERTIFICATE_PREPAREWITHORDERDETAIL.equals(cmd)){

            logger.info("执行验券准备请求，参数: {}", data == null ? "" : data.toJSONString());

            // 获取配置信息
            String appKey = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE, EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE_APPKEY);
            String appSecret = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE, EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE_APPSECRET);
            String merchantId = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE, EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE_MERCHANTID);
            if(appKey == null || "".equals(appKey) || appSecret == null || "".equals(appSecret) || merchantId == null || "".equals(merchantId)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            if(data == null){
                throw new DefaultException("参数缺失");
            }
            CertificatePrepareParamDTO dataDTO = JSONObject.parseObject(data.toString(), CertificatePrepareParamDTO.class);

            // 执行短信发送
            BaseResult resultDTO = kuaiShouLifeClient.prepareWithOrderDetail(appKey, appSecret, merchantId, dataDTO);

            logger.info("执行验券准备请求结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        // 验券
        if(EntranceConstant.ROUTER_KUAISHOU_CERTIFICATE_VERIFYWITHOFFLINESTORECODE.equals(cmd)){

            logger.info("执行验券请求，参数: {}", data == null ? "" : data.toJSONString());

            // 获取配置信息
            String appKey = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE, EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE_APPKEY);
            String appSecret = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE, EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE_APPSECRET);
            String merchantId = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE, EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE_MERCHANTID);
            String belong = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_CLIENT, EntranceConstant.SETTING_TYPE_CLIENT_BELONG);
            if(appKey == null || "".equals(appKey) || appSecret == null || "".equals(appSecret) || merchantId == null || "".equals(merchantId)  || belong == null || "".equals(belong)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            if(data == null){
                throw new DefaultException("参数缺失");
            }
            CertificateVerifyParamDTO dataDTO = JSONObject.parseObject(data.toString(), CertificateVerifyParamDTO.class);

            // 执行短信发送
            BaseResult resultDTO = kuaiShouLifeClient.verifyWithOfflineStoreCode(appKey, appSecret, merchantId, belong, dataDTO);

            logger.info("执行验券请求结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        // 撤销验券
        if(EntranceConstant.ROUTER_KUAISHOU_CERTIFICATE_CANCEL.equals(cmd)){

            logger.info("执行撤销验券请求，参数: {}", data == null ? "" : data.toJSONString());

            // 获取配置信息
            String appKey = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE, EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE_APPKEY);
            String appSecret = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE, EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE_APPSECRET);
            String merchantId = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE, EntranceConstant.SETTING_TYPE_KUAISHOU_LIFE_MERCHANTID);
            if(appKey == null || "".equals(appKey) || appSecret == null || "".equals(appSecret) || merchantId == null || "".equals(merchantId)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            if(data == null){
                throw new DefaultException("参数缺失");
            }
            CertificateCancelParamDTO dataDTO = JSONObject.parseObject(data.toString(), CertificateCancelParamDTO.class);

            // 执行短信发送
            BaseResult resultDTO = kuaiShouLifeClient.cancel(appKey, appSecret, merchantId, dataDTO);

            logger.info("执行撤销验券请求结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        throw new DefaultException("暂不支持此操作");
    }

}
